package scanner.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import pojo.Customer;
import scanner.DataScanner;

public class DataScannerImpl implements DataScanner {
	private InputStream in;
	private Scanner scaner;
	
	public DataScannerImpl(InputStream in) {
		super();
		this.in = in;
		scaner=new Scanner(in);
	}

	public List<Customer> scan() {
		List<String> list=preScan();
		List<Customer> list1=new ArrayList<Customer>();
		String reg = " ";
		String r = "[\\d]+ [a-zA-Z]+ [a-zA-Z0-9]+@(163|qq).com (girl|boy) \\d{4}-\\d{1,2}-\\d{1,2}";
		Pattern p = Pattern.compile(r);
		
		List<String> list2 = new ArrayList<String>();
		
		for(String i:list){
			Matcher m = p.matcher(i);
			if(m.find()){
				list2.add(i);
			}
		}
		for(String s:list2){
			String[] ss=s.split(reg); 			//剔除不合理数据,同时包括了切割数据和插入数据
			Customer c=new Customer();
			c.setId(Integer.parseInt(ss[0]));
			c.setName(ss[1]);
			c.setMail(ss[2]);
			c.setSex(ss[3]);
			c.setBirthday(ss[4]);
			
			list1.add(c);			//插入对象Customer到list<Customer>
		}
		
		return list1;
	}
	private List<String> preScan(){
		List<String> list=new ArrayList<String>();
		while(scaner.hasNext()){
			list.add(scaner.nextLine().replaceAll("\\s+"," "));
		}
		return list;
	}
	
	public static void main(String[] args) throws FileNotFoundException {
		DataScannerImpl dataScanner = new DataScannerImpl(new FileInputStream(new File("test.properties")) );
		List<Customer> list = dataScanner.scan();
		for(Customer i:list){
			i.toString();
		}
	}
}
